package com.mchat.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

@RequiredArgsConstructor
public class JsonUtil {
    @Autowired
    private final ObjectMapper objectMapper;

    public String toStr(Object t) {
        try {
            return objectMapper.writeValueAsString(t);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e);
        }
    }
    public  <T> T toObj(String str, Class<T> clz) {
        try {
            return objectMapper.readValue(str, clz);
        } catch (JsonProcessingException e) {
            throw new UnsupportedOperationException(e);
        }
    }
    public  <T> T toObj(JsonNode node, Class<T> clz) {
        return objectMapper.convertValue(node,clz);
    }

}
